<script setup>

import {onMounted, ref} from "vue";
import {showToast} from "vant";
import { areaList } from '@vant/area-data';
import {get, post} from "../../util/request.js";
import {useRoute, useRouter} from "vue-router";
import {useStore} from "../../pinia/index.js";

const searchResult = ref([]);
const router = useRouter();
const route = useRoute();
const store = useStore()
const addressEditInfo = ref({})
const title = ref("")
const showDelete = ref(true)
const onSave = (AddressEditInfo) => {
  console.log(AddressEditInfo)
  if(title.value=="新增地址"){
    post("/address/addAddAddress",AddressEditInfo).then(res=>{
      console.log(res)
      debugger
      console.log(res.data)
      showToast(res.data)
      addressEditInfo.value={}
    })
  }else if(title.value=="编辑地址"){
    post("/address/updateAddress",AddressEditInfo).then(res=>{
      console.log(res)
      console.log(res.data)
      showToast(res.data)
    })
  }else{
    showToast("错误");
  }


}
const onDelete = (AddressEditInfo) =>{
  post("address/deleteAddress",AddressEditInfo).then(res=>{
    if(res.code==0){
      showToast(res.data)
      addressEditInfo.value={}
    }else {
      showToast(res.data)
    }
  })
}
const onChangeDetail = (val) => {
  if (val) {
    searchResult.value = [
      {
        name: '黄龙万科中心',
        address: '杭州市西湖区',
      },
    ];
  } else {
    searchResult.value = [];
  }
};
onMounted(()=>{
  //  当使用 ref 来创建响应式变量时，你应该通过 .value 来读取和设置其内部的值
  addressEditInfo.value = store.currentAddress;
  title.value=route.query.title;
  if(title.value=="新增地址"){
    showDelete.value=false
  }
  if(title.value=="编辑地址"){
    get("address/getAddressByAddressId",{id:addressEditInfo.value.id}).then(res=>{



       addressEditInfo.value = {id:res.data.addressId,
      userId:res.data.userId,
      name:res.data.name,
      tel:res.data.phoneNumber,
      province:res.data.province,
      city:res.data.city,
      county:res.data.district,
      addressDetail:res.data.detailAddress,
      isDefault:res.data.isDefault == 1}
      if(res.code==0){

        store.setCurrentAddress({id:res.data.addressId,
          userId:res.data.userId,
          name:res.data.name,
          tel:res.data.phoneNumber,
          province:res.data.province,
          city:res.data.city,
          county:res.data.district,
          addressDetail:res.data.detailAddress,
          isDefault:res.data.isDefault})
      }else {
        showToast("加载失败")
      }
    })
  }
})
const onClickLeft = ()=> history.back();

</script>

<template>
  <van-nav-bar
      :title="title"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
  />

<!--  <h1>abc</h1>-->
    <van-address-edit
        :area-list="areaList"
        :show-delete="showDelete"
        :address-info="addressEditInfo"
        show-set-default
        show-search-result
        :search-result="searchResult"
        :area-columns-placeholder="['请选择', '请选择', '请选择']"
        @save="onSave"
        @delete="onDelete"
        @change-detail="onChangeDetail"
    />


</template>

<style scoped>

</style>